package com.appbee.gdata.youtube.client.search;

import javax.inject.Singleton;

import com.google.gwt.core.client.GWT;
import com.google.gwt.jsonp.client.JsonpRequestBuilder;

@Singleton
public class YoutubeService
{
	private String baseUrl = "http://gdata.youtube.com/feeds/api/videos?v=2&alt=json-in-script&q=";
	private String fields = "&fields=openSearch:totalResults,entry(id,title,content,author,published,media:group(media:content[@isDefault='true'],media:thumbnail[@height='90']))";
	
	public void findByFilter(YoutubeFilter filter, final FeedCallback handler)
	{
		StringBuffer url = new StringBuffer();
		url.append(baseUrl);
		url.append(filter.getQuery());
		url.append("&start-index=");
		url.append(filter.getStartIndex());
		url.append("&max-results=");
		url.append(filter.getMaxResult());
		url.append(fields);
		GWT.log("Youtube query: " + url.toString());
		JsonpRequestBuilder rb = new JsonpRequestBuilder();
		rb.setCallbackParam("callback");
		rb.requestObject(url.toString(), handler);
	}
	
}
